#include "localroster.h"

LocalRoster::LocalRoster(QObject *parent) :
    QObject(parent)
{
}


void LocalRoster::addRoster(XMPPRoster *roster)
{
    connect(roster, SIGNAL(itemAdded(JID)), this, SLOT(itemAdded(JID)));
    connect(roster, SIGNAL(itemRemoved(JID)), this, SLOT(itemRemoved(JID)));
    connect(roster, SIGNAL(itemSubscribed(JID)), this, SLOT(itemSubscribed(JID)));
    connect(roster, SIGNAL(itemUnsubscribed(JID)), this, SLOT(itemUnsubscribed(JID)));
    connect(roster, SIGNAL(itemUpdated(JID)), this, SLOT(itemUpdated(JID)));
    connect(roster, SIGNAL(rosterRecieved(Roster)), this, SLOT(rosterRecieved(Roster)));
    connect(roster, SIGNAL(rosterPresence(RosterItem,QString,Presence::PresenceType,QString)),
            this, SLOT(rosterPresence(RosterItem,QString,Presence::PresenceType,QString)));
    connect(roster, SIGNAL(rosterError(IQ)), this, SLOT(rosterError(IQ)));
    connect(roster, SIGNAL(nonrosterPresence(Presence)), this, SLOT(nonrosterPresence(Presence)));
    connect(roster, SIGNAL(unsubscriptionRequest(JID,QString)), this, SLOT(unsubscriptionRequest(JID,QString)));
    connect(roster, SIGNAL(subscriptionRequest(JID,QString)), this, SLOT(subscriptionRequest(JID,QString)));
    connect(roster, SIGNAL(selfPresence(RosterItem,QString,Presence::PresenceType,QString)), this, SLOT(selfPresence(RosterItem,QString,Presence::PresenceType,QString)));
}

void LocalRoster::removeRoster(XMPPRoster *roster)
{
    //TODO: Implement removeRoster!
}

void LocalRoster::addRosterManager(RosterManager *manager)
{
    managers.append(manager);
}

void LocalRoster::removeRosterManager(RosterManager *manager)
{
    managers.removeOne(manager);
}

// Called when item added to roster on server side
void LocalRoster::itemAdded(const JID &jid)
{
    qDebug() << "[Roster] added: " << jid.full().c_str();
    XMPPRoster *srcRoster = dynamic_cast<XMPPRoster*>(sender());
    if (srcRoster)
        emit itemAdded(jid, srcRoster->getClientJID());
}

// Called when item removed to roster on server side
void LocalRoster::itemRemoved(const JID &jid)
{
    qDebug() << "[Roster] removed: " << jid.full().c_str();
    XMPPRoster *srcRoster = dynamic_cast<XMPPRoster*>(sender());
    if (srcRoster)
        emit itemRemoved(jid, srcRoster->getClientJID());
}

// Called when entity subscribed to our client on server side
void LocalRoster::itemSubscribed(const JID &jid)
{
    qDebug() << "[Roster] subscribed: " << jid.full().c_str();
    XMPPRoster *srcRoster = dynamic_cast<XMPPRoster*>(sender());
    if (srcRoster)
        emit itemSubscribed(jid, srcRoster->getClientJID());
}

// Called when entity unsubscribed to our client on server side
void LocalRoster::itemUnsubscribed(const JID &jid)
{
    qDebug() << "[Roster] unsubscribed: " << jid.full().c_str();
    XMPPRoster *srcRoster = dynamic_cast<XMPPRoster*>(sender());
    if (srcRoster)
        emit itemUnsubscribed(jid, srcRoster->getClientJID());
}

// Called when item updated to roster on server side
void LocalRoster::itemUpdated(const JID &jid)
{
    qDebug() << "[Roster] updated: " << jid.full().c_str();
    XMPPRoster *srcRoster = dynamic_cast<XMPPRoster*>(sender());
    if (srcRoster)
        emit itemUpdated(jid, srcRoster->getClientJID());
}

void LocalRoster::rosterRecieved(const Roster &roster)
{
    qDebug() << "[LocalRoster] Roster Recieved: ";

    RosterItemPair p;
    foreach(p, roster)
    {
        qDebug() << " + " << p.first.c_str() << ": online = " << p.second->online();

    }

    XMPPRoster *srcRoster = dynamic_cast<XMPPRoster*>(sender());
    if (srcRoster)
        emit rosterRecieved(roster, srcRoster->getClientJID());
}

void LocalRoster::rosterPresence(const RosterItem &item,
                                 const QString &resource,
                                 Presence::PresenceType presence,
                                 const QString &msg)
{
    qDebug() << "[Roster] presense: " << item.jid().c_str()
             << "; resource: "<< resource
             << "; presence " << presence
             << "; msg: " << msg;

    XMPPRoster *srcRoster = dynamic_cast<XMPPRoster*>(sender());
    if (srcRoster)
        emit rosterPresence(item, resource, presence, msg, srcRoster->getClientJID());
}

void LocalRoster::rosterError(const IQ &iq)
{
    qDebug() << "[Roster] error: " << iq.error()->text().c_str();

    XMPPRoster *srcRoster = dynamic_cast<XMPPRoster*>(sender());
    if (srcRoster)
        emit rosterError(iq, srcRoster->getClientJID());
}

void LocalRoster::nonrosterPresence(const Presence &presence)
{
    qDebug() << "[Roster] nonroster presence: type = " << presence.presence();

    XMPPRoster *srcRoster = dynamic_cast<XMPPRoster*>(sender());
    if (srcRoster)
        emit nonrosterPresence(presence, srcRoster->getClientJID());
}

void LocalRoster::unsubscriptionRequest(const JID &jid, const QString &msg)
{
    qDebug() << "[Roster] unsubscription request: " << jid.full().c_str()
            << "; msg = " << msg;

    XMPPRoster *srcRoster = dynamic_cast<XMPPRoster*>(sender());
    if (srcRoster)
        emit unsubscriptionRequest(jid, msg, srcRoster->getClientJID());
}

void LocalRoster::subscriptionRequest(const JID &jid, const QString &msg)
{
    qDebug() << "[Roster] subscription request: " << jid.full().c_str()
            << "; msg = " << msg;

    XMPPRoster *srcRoster = dynamic_cast<XMPPRoster*>(sender());
    if (srcRoster)
        emit subscriptionRequest(jid, msg, srcRoster->getClientJID());
}

void LocalRoster::selfPresence(const RosterItem &item,
                               const QString &resource,
                               Presence::PresenceType presence,
                               const QString &msg)
{
    qDebug() << "[Roster] self presense: " << item.jid().c_str()
             << "; resource: "<< resource
             << "; presence " << presence
             << "; msg: " << msg;

    XMPPRoster *srcRoster = dynamic_cast<XMPPRoster*>(sender());
    if (srcRoster)
        emit selfPresence(item, resource, presence, msg, srcRoster->getClientJID());
}


